#coding:utf-8
from django.contrib import auth
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.import forms
from django.contrib.auth.forms import UserCreationForm

def login_view(request):
    '''登录视图'''
    username = request.POST.get("username", "")
    password = request.POST.get("password", "")
    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth.login(request, user)
        return HttpResponseRedirect('/mylogs')
    else:
        return HttpResponseRedirect('/accounts/register')

def logout_view(request):
    pass

def profile(request):
    pass

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect('/mylogs/')
    else:
        form = UserCreationForm()
    return render_to_response("accounts/register.html",
                              {'form':form,})
